package com.bw.wxy.mvp.presenter;

import com.bw.wxy.mvp.base.prsenter.BasePresenter;
import com.bw.wxy.mvp.contract.LREContract;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.factroy.HttpCode;
import com.bw.wxy.mvp.model.entity.NewsEntity;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LREPresenter extends BasePresenter<LREContract.ILREModel, LREContract.ILREView> {
    private int page=1;
    private loadMoreOb ob;

    @Inject
    public LREPresenter(LREContract.ILREModel mModel, LREContract.ILREView mView) {
        super(mModel, mView);
        ob=new loadMoreOb(this);
    }

    //请求全部数据
    public void requestAll(Map<String,Object>...maps){
        for (int i = 0; i < maps.length; i++) {
            int code = (int) maps[i].get("code");
            switch (code) {
                case HttpCode.CHANNELNEWS:
                    maps[i].put("page",page);
                    break;
                case HttpCode.FOLLOWNEWSCODE:
                    maps[i].put("page",page);
                    break;
                case HttpCode.USERFOLLOWCODE:
                    maps[i].put("page",page);
                    break;
                case HttpCode.VIDEONEWSCODE:
                    maps[i].put("page",page);
                    break;
            }
        }
        mModel.requestAll(maps).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    //刷新数据
    public void requestRefresh(Map<String,Object>map){
        page=1;
        map.put("page",page);
        mModel.requestRefreshLoad(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    //加载数据
    public void requestLoad(Map<String,Object>map){
        page+=1;
        map.put("page",page);
        mModel.requestRefreshLoad(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(ob);
    }



    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
        if (baseEntity instanceof NewsEntity){
            mView.refreshRecyclerView(baseEntity);
            return;
        }
        mView.refreshAll(baseEntity);
    }

    public static class loadMoreOb implements Observer<BaseEntity>{
        private LREPresenter lrePresenter;

        public loadMoreOb(LREPresenter lrePresenter){
            this.lrePresenter=lrePresenter;
        }

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            lrePresenter.onSubscribe(d);
        }

        @Override
        public void onNext(@NonNull BaseEntity baseEntity) {
            lrePresenter.loadMoreNext(baseEntity);
        }

        @Override
        public void onError(@NonNull Throwable e) {
            lrePresenter.onError(e);
        }

        @Override
        public void onComplete() {
            lrePresenter.onComplete();
        }
    }

    public void loadMoreNext(BaseEntity entity){
        mView.loadMoreRecyclerView(entity);
    }
}

